package br.pucrs.lognplay.faces.bean;

import java.util.Map;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.pucrs.lognplay.entity.User;
import br.pucrs.lognplay.servlet.SecurityFilter;

public abstract class Bean {

	protected void addInformationMessage(String summary, String detail) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		currentInstance.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, summary, detail));
	}

	protected void addWarningMessage(String summary, String detail) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		currentInstance.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, summary, detail));
	}

	protected void addErrorMessage(String summary, String detail) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		currentInstance.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, detail));
	}

	protected void addFatalMessage(String summary, String detail) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		currentInstance.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, detail));
	}

	protected MethodExpression createActionExpression(String expression) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		Application application = currentInstance.getApplication();
		ExpressionFactory expressionFactory = application.getExpressionFactory();
		ELContext elContext = currentInstance.getELContext();

		return expressionFactory.createMethodExpression(elContext, expression, String.class, new Class<?>[0]);
	}

	protected String getParameterValue(String parameterName) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		ExternalContext externalContext = currentInstance.getExternalContext();
		Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
		return requestParameterMap.get(parameterName);
	}

	protected User getLoggedUser() {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		ExternalContext externalContext = currentInstance.getExternalContext();
		HttpSession httpSession = (HttpSession) externalContext.getSession(true);
		return (User) httpSession.getAttribute(SecurityFilter.LOGGED_USER_ATTRIBUTE);
	}

	protected void loginUser(User user) {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		ExternalContext externalContext = currentInstance.getExternalContext();
		HttpSession httpSession = (HttpSession) externalContext.getSession(true);
		httpSession.setAttribute(SecurityFilter.LOGGED_USER_ATTRIBUTE, user);
	}

	protected void logoutUser() {
		FacesContext currentInstance = FacesContext.getCurrentInstance();
		ExternalContext externalContext = currentInstance.getExternalContext();
		externalContext.invalidateSession();
	}
}
